package com.ydw;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author Yangdw
 * @date 2025-09-01
 * @description:
 */
public class Subscribe01MultiConsumer {

    public static void main(String[] args) throws MQClientException {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("test-filter-multi-group",true);



        consumer.setNamesrvAddr("127.0.0.1:9876");

        consumer.subscribe("custom-filter-topic","Buy || Pay");
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        consumer.registerMessageListener(new MessageListenerOrderly() {
            @Override
            public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) {
                    for (MessageExt messageExt : msgs) {
                        System.out.println("收到的时间:"+dateTimeFormatter.format(LocalDateTime.now()));
                        System.out.println("收到消息:"+new String(messageExt.getBody()));
                    }

                    return ConsumeOrderlyStatus.SUCCESS;
            }
        });

        consumer.start();
    }
}
